/**
 * @author Vanch
 */
;(function() {
	'use strict';
	window.MyAjax ={
		is_use_common_url:true,
		server:"http://114.242.206.226:8787"
	};	
	
	MyAjax.ajax = function (params) {
			if(MyAjax.is_use_common_url){
				var url = params['url'];
				if(url.indexOf("http")>0){
					//use customer's url
				}else{
					url = MyAjax.server +params['url'];
					params['url'] = url;
				}
			}
			$.ajax(params);
	};		
	
	MyAjax.ajaxWithErrorProc = function (params) {				
			var success = params['success'];
			var error = params['error'];
			params['dataType'] = "json";
			params['success'] = function(dat){
				if(dat && dat.ret == 1){
				}else{
					error(dat);
					alert(dat.message);
					return;
				}
				success(dat);
			};
			params['error']=function (xhr, type, exception){
				var dat = {};
				dat['xhr'] = JSON.stringify(xhr);
				dat['type'] = type;
				dat['exception'] = JSON.stringify(exception);
				if(type == "abort"){
					dat['ret'] = 99999;// to hide abort tips, by if(dat.ret)
				}
				error(dat);
			};
			MyAjax.ajax(params);
	};
	
	//only use this one!!!! by Vanch
	MyAjax.ajaxBackOkError = function (params, successFunc, errorFunc) {
			var hasSuccessFunc = successFunc?true:false;
			var hasErrorFunc = errorFunc?true:false;
			if((successFunc && hasErrorFunc) == false){
				throw Exception("need successFunc and errorFunc");
				return;
			}
			console.log(JSON.stringify(params['data']));//test!!!
			params['dataType'] = "json";
			params['type'] = "GET";
			params['success'] = function(dat){
				//successFunc return true, means the response is resolved.
				//successFunc return true, means to do default process.
				if(successFunc(dat)){
					return;
				}
				
				//TODO default process.
				if(dat && dat.ret == 1){
					
				}else{
					alert(dat.des);
					return;
				}
			};
			params['error']=function (xhr, type, exception){
				console.log("MyAjax error type:" +type);
				//errorFunc return true, means the response is resolved.
				//errorFunc return true, means to do default process.
				//alert("获取数据失败[500 or Network Error]");
				
				//TODO default process.
				var dat = {};
				dat['xhr'] = JSON.stringify(xhr);
				dat['type'] = type;
				dat['exception'] = JSON.stringify(exception);
				if(type == "abort"){
					dat['ret'] = 99999;// to hide abort tips, by if(dat.code)
				}else{
					alert(dat['exception']);
				}
				if(errorFunc(dat)){
					return;
				}
			};
			MyAjax.ajax(params);
	};
		
})(Zepto);